package com.barnett.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 * 基础的ListView的Adapter
 * 
 * @author jiangjian
 */
public abstract class BaseMyListViewAdapter<T> extends BaseAdapter {

	private ArrayList<T> list;
	private final SparseArray<View> views= new SparseArray<View>();
	
	public BaseMyListViewAdapter(ArrayList<T> list) {
		this.list = list;

	}

	@Override
	public void notifyDataSetChanged() {
		// TODO Auto-generated method stub
		super.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@SuppressWarnings({ "unchecked", "hiding" })
	public <T extends View> T findViewById(View convertView,int resourceId) {
//		View view = views.get(resourceId);
//		if (view == null) {
		View view = convertView.findViewById(resourceId);
		views.put(resourceId, view);
//		}
		return (T) view;
	}
	
	public View getConvertView(Context context, View convertView,
			ViewGroup parent, int res) {
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(res, parent,
					false);
		}
		return convertView;
	}
	
	@Override
	public View getView(int arg, View convertView, ViewGroup parent) {
		convertView = getView(list, arg, convertView, parent);
		return convertView;
	}

	public abstract View getView(ArrayList<T> list,
			int position, View convertView, ViewGroup parent);
}